/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.factoryservice.resourcetypes
{	
	import com.screenworker.compactmvc.extensions.factoryservice.events.FactoryServiceEvent;
	import com.screenworker.compactmvc.extensions.interfaces.IResourceType;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;

	public class ResourceFrameTwo extends EventDispatcher implements IResourceType
	{
		public static const NAME:String = 'Frame';
		private var _targetMc:MovieClip;
		private var _progress:Number = 0;
		private var _name:String;
		private var _isComplete:Boolean;
		private var _className:String;
		
		public function ResourceFrameTwo(name:String, targetMc:MovieClip, className:String)
		{
			super();
			_name = name;
			_isComplete = false;
			_targetMc = targetMc;
			_className = className;	
		}
		
		public function reset():void
		{
			stopLoading();
			_targetMc = null;
		}
		
		/**
		 * loop event  
		 * @param event
		 * 
		 */		
		private function enterFrameHandler(event:Event):void 
		{
			_progress = _targetMc.root.loaderInfo.bytesLoaded / _targetMc.root.loaderInfo.bytesTotal;
			
            if(_targetMc.framesLoaded >= 2) 
            {   
              _progress = 1;
              //try to get Class in frame two
              if(isClassAccessible)
              {
              	 stopLoading();
              	 _isComplete = true;
              	 dispatchEvent(new FactoryServiceEvent(FactoryServiceEvent.COMPLETE, resourceName) );
              }                    
            }          
        }
        
        public function get isClassAccessible():Boolean
        {
        	//jump to frame two
        	if(_targetMc.currentFrame == 1)
        		_targetMc.nextFrame();  
        	//  
            try
            {
            	var applicationMainClass:Class = getDefinitionByName(_className) as Class;
                return true;       
            }
            catch(e:Error)
            {
            	return false;
            }
            return false;
        }
        
        /**
         * starts loading 
         * 
         */        
        public function startLoading():void
        {
			_targetMc.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
        }
        
        /**
         * stops loading 
         * 
         */        
        public function stopLoading():void
        {
        	_targetMc.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); 
        }
           
        /**
         * Returns loading progress
         * @return 
         * 
         */
        public function get progress():Number
        {
        	return _progress;
        }
        
        public function set progress(value:Number):void
		{
			_progress = value;
		}
        
        /**
         * returns resourceName
         * @return 
         * 
         */        
        public function get resourceName():String
        {
        	return _name;
        }
        
        /**
         * returns complete status
         * @return 
         * 
         */ 
        public function get isComplete():Boolean
        {
        	return _isComplete;
        }
        
        public function set isComplete(value:Boolean):void
        {
        	if(value)
        		stopLoading();
        	_isComplete = value;
        }
        
        /**
        * returns the classReference
        * @usage: set Flex project Compiler Options (Project->Properties->ActionScript Compiler->Additional compiler arguments)
        * -frame two com.myproject.ApplicationMain
        * */
        public function get classReference():Class 
       	{
       		if(!_isComplete)
       			return null;
       		
       		if(!isClassAccessible)
       			return null;
       
            return getDefinitionByName(_className) as Class; 
          
        }
        
        public function get applicationDomain():ApplicationDomain
       	{
       		return null;
       	}
        
	}
}
